package com.zyw.service.production.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.*;
import com.zyw.from.ProductionFrom;
import com.zyw.mapper.production.ProductionManagementMapper;
import com.zyw.service.order.OrderService;
import com.zyw.service.procurement.ProcurementMaterialAnalysisService;
import com.zyw.service.procurement.ProcurementOrdersService;
import com.zyw.service.production.ProductionManagementService;
import com.zyw.service.repertory.RepertoryFinishService;
import com.zyw.service.repertory.RepertoryMaterialService;
import com.zyw.service.repertory.impl.RepertoryMaterialServiceImpl;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class ProductionManagementServiceImpl implements ProductionManagementService {

    @Resource
    public ProductionManagementMapper productionManagementMapper;

    @Autowired
    public OrderService orderService;

    @Autowired
    public RepertoryMaterialService repertoryMaterialService;

    @Autowired
    private ProcurementMaterialAnalysisService procurementMaterialAnalysisService;
    @Autowired
    private ProcurementOrdersService procurementOrdersService;
    @Autowired
    public RepertoryFinishService repertoryFinishService;

    @Override
    @Transactional
    public void save(ProductionManagement productionManagement) {
        productionManagementMapper.insertSelective(productionManagement);

        //修改分析表状态
        ProcurementOrders procurementOrders = new ProcurementOrders();
        procurementOrders.setId(productionManagement.getOrderId());
        procurementOrders.setProductionStatus(1);
        procurementOrdersService.updateStatus(procurementOrders);

        //修改订单状态
        Order order = new Order();
        order.setId(productionManagement.getOrderId());
        order.setStatus(3);
        orderService.update(order);
        //修改库存信息

        List<ProcurementMaterialAnalysis> procurementMaterialAnalyses = procurementMaterialAnalysisService.selectByOrderId(productionManagement.getOrderId());
        for (ProcurementMaterialAnalysis procurementMaterialAnalysis : procurementMaterialAnalyses) {
            repertoryMaterialService.update(procurementMaterialAnalysis.getLiningId(),procurementMaterialAnalysis.getLiningNum(),procurementMaterialAnalysis.getOrderId());
        }



    }

    @Override
    public LayUITableVO<List<ProductionManagement>> findPage(Map<String, Object> map) {
        ProductionFrom from = (ProductionFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(ProductionManagement.class);
        Example.Criteria c = example.createCriteria();


        if (! "".equals(from.getPrincipal()) && null!=from.getPrincipal()){
            c.andLike("principal","%"+from.getPrincipal()+"%");
        }
        if (! "".equals(from.getProductionId()) && null!=from.getProductionId()){
            c.andLike("id","%"+from.getProductionId()+"%");
        }
        List<ProductionManagement> list = productionManagementMapper.selectByExample(example);

        for (ProductionManagement productionManagement : list) {
//            productionManagement.setStartDate(UnitConversionUtil.conversionDate(productionManagement.getStartDate()));
//            productionManagement.setEndDate(UnitConversionUtil.conversionDate(productionManagement.getEndDate()));
            if (productionManagement.getStatus() == 0){
                productionManagement.setStatusString("生产中");
            }else {
                productionManagement.setStatusString("已完成");
            }

        }

        PageInfo<ProductionManagement> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ProductionManagement>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public ProductionManagement selectById(String id) {

        ProductionManagement productionManagement = productionManagementMapper.selectByPrimaryKey(id);
        String schedule = productionManagement.getSchedule();
        Integer one = Integer.valueOf(schedule.substring(0, schedule.indexOf("/")));
        Integer two = Integer.valueOf(schedule.substring(schedule.indexOf("/")+1));
        Integer diff = two-one;
        productionManagement.setDiff(diff);


        return productionManagement;
    }

    @Override
    @Transactional
    public void updateSchedule(ProductionManagement productionManagement) {
        Integer nowDay = Integer.valueOf(productionManagement.getNowDay());
        String schedule = productionManagement.getSchedule();
        Integer one = Integer.valueOf(schedule.substring(0, schedule.indexOf("/")));
        Integer two = Integer.valueOf(schedule.substring(schedule.indexOf("/")+1));
        one = one+nowDay;
        productionManagement.setSchedule(one+"/"+two);

        productionManagementMapper.updateByPrimaryKeySelective(productionManagement);
    }

    @Override
    @Transactional
    public void updateStatus(RepertoryFinish repertoryFinish) {
        ProductionManagement productionManagement = new ProductionManagement();
        productionManagement.setId(repertoryFinish.getProductionId());
        productionManagement.setStatus(1);
        productionManagementMapper.updateByPrimaryKeySelective(productionManagement);


        repertoryFinishService.save(repertoryFinish);

    }
}
